<template>
	<view class="defaultStyles">
	</view>
</template>
<script lang="uts">
	import {
		UIView,
		UIControl,
		UIApplication,
		CGRect
	} from "UIKit"


	import { URL, NSDictionary, CGFloat, CGSize } from "Foundation";
	import { AVLayerVideoGravity } from 'AVFoundation'



	//原生提供以下属性或方法的实现  
	export default {
		name: "camera-view",
		emits: ['onPictureTaken'],
		props: {
			"facing": {
				type: String,
				default: "front"
			},
			"flash": {
				type: String,
				default: 'off'
			},
			"mode": {
				type: String,
				default: 'photo'
			},
		},
		watch: {
			"facing": {
				/**
				 * 这里监听属性变化，并进行组件内部更新
				 */
				handler(newVal : string, oldVal : string) {
					"[weak self]"
					this?.updateFacing(newVal)
				},
				immediate: false //创建时是否通过此方法更新属性，默认值为false  
			},
			"flash": {
				/**
				 * 这里监听属性变化，并进行组件内部更新
				 */
				handler(newVal : string, oldVal : string) {
					"[weak self]"
					this?.updateFlash(newVal)
				},
				immediate: false //创建时是否通过此方法更新属性，默认值为false  
			},
			"mode": {
				/**
				 * 这里监听属性变化，并进行组件内部更新
				 */
				handler(newVal : string, oldVal : string) {
					"[weak self]"
					this?.changeMode(newVal)
				},
				immediate: false //创建时是否通过此方法更新属性，默认值为false  
			},
		},
		data() {
			return {
				previewView: null as CKFPreviewView | null,
				photoSession: null as CKFPhotoSession | null,
				videoSession: null as CKFVideoSession | null,
			}
		},
		expose: ['close', 'open', 'takePicture', 'takePictureSnapshot', 'takeVideo', 'stopVideo', 'setZoom'],
		methods: {

			updateFacing(val : string) {
				if (this.mode == 'video') {
					this.videoSession!.cameraPosition = val == 'back' ? CKFSession.CameraPosition.back : CKFSession.CameraPosition.front;
				} else {
					this.photoSession!.cameraPosition = val == 'back' ? CKFSession.CameraPosition.back : CKFSession.CameraPosition.front;
				}


			},
			updateFlash(val : string) {
				let flashMode : CKFSession.FlashMode;

				switch (val) {
					case 'on':
						flashMode = CKFSession.FlashMode.on;
						break;
					case 'auto':
						flashMode = CKFSession.FlashMode.auto;
						break;
					case 'off':
						flashMode = CKFSession.FlashMode.off;
						break;
					default:
						flashMode = CKFSession.FlashMode.auto;
						break;
				}

				if (this.mode == 'video') {
					this.videoSession!.flashMode = flashMode;
				} else {
					this.photoSession!.flashMode = flashMode;
				}
			},

			close() {
				if (this.mode == 'video') {
					this.videoSession!.stop();
				} else {
					this.photoSession!.stop();
				}
			},
			open() {
				if (this.mode == 'video') {
					this.videoSession!.start();

				} else {
					this.photoSession!.start();
				}
			},
			/**
			 * 对外公开的组件方法
			 */
			takePicture() {
				CKFHelper.takePhoto(this.photoSession!, (res) => {
					this.$emit('onPictureTaken', res)
				})
			},
			takePictureSnapshot() {
				CKFHelper.takePhoto(this.photoSession!, (res) => {
					this.$emit('onPictureTaken', res)
				})
			},
			takeVideo() {

				if (this.isRecording()) {
					return
				}
				this.$emit('onVideoRecordingStart')

				CKFHelper.takeVideo(this.videoSession!, (res) => {
					this.$emit('onVideoRecordingEnd', res)
				})

			},
			stopVideo() {
				if (this.isRecording()) {
					this.videoSession!.stopRecording();
				}
			},
			setZoom(zoom : Double) {
				if (this.mode == 'video') {
					this.videoSession!.zoom = zoom;

				} else {
					this.photoSession!.zoom = zoom;
				}
			},
			isRecording() : boolean {
				return this.videoSession!.isRecording;
			},

			changeMode(type : string) {
				if (mode == 'video') {
					if (this.videoSession == null) {
						this.videoSession = CKFVideoSession()
					}
					this.previewView!.session = this.videoSession;
				} else {
					if (this.photoSession == null) {
						this.photoSession = CKFPhotoSession()
					}
					this.previewView!.session = this.photoSession;
				}

			}


		},
		/**
		 * 创建原生View，必须定义返回值类型
		 */
		NVLoad() : CKFPreviewView {
			//必须实现  
			let previewView = CKFPreviewView()
			this.previewView = previewView;
			return previewView
		},
		NVLoaded() {

			const frame = this.previewView!.frame;
			const size = CGSize(width = frame.width * 5, height = frame.height * 5)

			this.changeMode(this.mode)

			this.previewView!.autorotate = true;
			this.previewView!.previewLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill;

			if (this.mode == 'video') {
				// (this.previewView!.session as CKFVideoSession).setWidth;
			} else {
				(this.previewView!.session as CKFPhotoSession).resolution = size;

			}

			this.updateFacing(this.facing)
			this.updateFlash(this.flash)

			this.open()


		},
		unmounted() {
			console.log('unmounted')
			if (this.previewView != null && this.previewView!.session != null) { // 如果组件绑定了视图则需要在组件销毁时释放视图相关资源
				this.previewView!.session = null;
				if (this.photoSession != null) {
					this.photoSession = null;
				}
				if (this.videoSession != null) {
					this.videoSession = null;
				}
			}
		},
		NVUnloaded() {
			console.log('NVUnloaded')
		},
	}
</script>

<style>
	.defaultStyles {
		width: 750rpx;
		/* height: 240rpx; */
	}
</style>